<?php 
// +----------------------------------------------------------------------
// | WoShiMaiJia Projcet 
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://woshimaijia.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: xinqiyang <517577550@qq.com>
// +----------------------------------------------------------------------

/**
 * Admin BaseApp class
 * @author xinqiyang (xinqiyang@gmail.com)
 *
 */
class AdminBaseAction  extends BaseAction
{
	public function _initialize()
	{
		//判断登陆状态
//		$admin = Session::get('admin');
//		if(empty($admin))
//		{
//			//$this->redirect('login/index');
//		} 

		//加载左边菜单
		$array = array(0=>array('module'=>'About','modulename'=>'About模块'));
        $this->assign('leftBar',$array);
		
	}
	//定义预处理函数
	
	protected function _bf_insert($fn) { }
	protected function _bf_update($fn) { }
	protected function _bf_show($fn) { }
	protected function _af_update($fn) { }
	protected function _af_insert($fn) { }
	
	
	/**
	 * auto show the function
	 * make sure the template file in the Module document
	 * 
	 */
	protected function show()
	{
		if(method_exists($this,'_bf_show'))
		{
			$this->_bf_show(ACTION_NAME);
		}
		$this->display();
	}
	
	
	
	/**
	 * Base Insert function
	 * @param $model string modelname
	 */
	protected function insert($model='') {
		if($model)
		{
			$model = D("$model");
			if($model->create())
			{
				$id = $model -> add();
				if($id)
				{
					if(method_exists($this,'_af_insert'))
					{
						$this->_af_insert($id);
					}
					$_POST['jump_url'] = isset($_POST['jump_url'])? $_POST['jump_url']:"__URL__/index";
					$this->assign('jumpUrl',$_POST['jump_url']);
					$this->success("Success!!");
				} else {
					$this->error("Get Error!");
				}
			} else {
				$this->error($model->getError());
			}
		} else {
			$this->error("No Model in this Action");
		}

	}

	
	/**
	 * Base Update function
	 * @param $model string modelname
	 */
	protected function update($model='') {
		if($model)
		{
			if(method_exists($this,'_bf_update'))
			{
				$this->_bf_update(ACTION_NAME);
			}
			$model = D("$model");
			$pk = $model->getpk();
			if($model->create())
			{
				//赋主键值
				//$model->$pk = $_REQUEST['id'];
				//TODO:volidate data
				$result = $model->save();
				//dump($model->getLastSQL()); exit;
				if($result)
				{
					if(method_exists($this,'_af_update'))
					{
						$this->_af_update(ACTION_NAME);
					}
					$_POST['jump_url'] = isset($_POST['jump_url'])? $_POST['jump_url']:"__URL__/index";
					$this->assign('jumpUrl',$_POST['jump_url']);
					$this->success('Success!^_^');
				} else {
					$this->error('Save Error');
				}
			} else {
				$this->error('Validate false');
			}
		} else {
			$this->error("Model error!");
		}
		
	}

	

}
?>